home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / Demos / FlickeringGrating.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-07  |  6.1 KB  |  204 lines  |  [TEXT/KAHL]

  1. /*
  2. FlickeringGrating.c
  3. This demo displays a research-grade visual stimulus. If you just want to know
  4. how to load the clut and display a pattern, then you should start by reading
  5. Grating.c, which is much shorter and simpler.
  6. Copyright (c) 1989-1993 Denis G. Pelli
  7. HISTORY:
  8. 11/89     Lan & Denis wrote it.
  9. 23.1.90    dgp        Use second screen only if available.
  10. 4/9/90    dgp        Changed WindowPtr to CWindowPtr. Made big arrays static. Reduced
  11.                 default memory allocation to 1 megabyte. Use any 8-bit screen,
  12.                 preferably not the main screen.
  13. 4/23/90    dgp        Added optional timing. Centered the image. Left clut entries 0 and 
  14.                 clutSize-1 alone, so background doesn't flash. Asked if ISR Video 
  15.                 Attenuator is present.
  16. 10/11/90 dgp    Added fpu test.
  17. 10/29/90 dgp    Added CenterRectInRect().
  18. 10/30/90 dgp    Changed call to SetLuminances() to SetLuminancesAndRange() so that
  19.                 the range is now kept fixed throughout all the frames, to avoid
  20.                 flashes.
  21. 8/24/91    dgp        Made compatible with THINK C 5.0.
  22.                 If possible, use ReadLuminanceRecord().
  23. 3/10/92    dgp        include mc68881.h
  24. 4/26/92    dgp        RestoreCluts().
  25. 8/27/92    dgp        replace SysEnvirons() by Gestalt()
  26. 12/30/92 dgp    made more like Filter, using a small console so that, if necessary,
  27.                 both the console and the grating will fit on the main monitor.
  28. 2/7/93    dgp        replaced SetOnePixel by SetPixelsQuickly
  29. 7/7/93    dgp        added code for compatibility with Radius PowerView, a SCSI video box.
  30. */
  31. #include "VideoToolbox.h"
  32. #include "Luminance.h"
  33. #include <math.h>
  34. #if THINK_C
  35.     #include <console.h>
  36.     #include <profile.h>
  37. #endif
  38.  
  39. #define SIZE         300            // size of grating, in pixels
  40. #define TMAX        3*67        // duration, in frames, typically at 67 Hz
  41. #define REPETITIONS    5            // Number of times to repeat the animation
  42. #define PROFILE        0            // optionally, report timing
  43.  
  44. void main(void);
  45. void FlickeringGrating(void);
  46. typedef struct{
  47.     ColorSpec table[256];
  48. }ColorSpecTable;
  49.  
  50. void main(void)
  51. {
  52.     Require(gestalt8BitQD);
  53.     FlickeringGrating();
  54. }
  55.     
  56. void FlickeringGrating(void)
  57. {
  58.     register int i;
  59.     int j,tmax,pixelSize,clutSize;
  60.     static double fX[SIZE],fY[SIZE];
  61.     double LMid,LMin,LMax,dL,a,contrast;
  62.     WindowPtr window=NULL, oldWindow=NULL;
  63.     GDHandle device=NULL,oldDevice;
  64.     static luminanceRecord LR;
  65.     static ColorSpecTable *tables[TMAX];    // an array of pointers to ColorSpec tables
  66.     static char string[100];
  67.     Rect r;
  68.     Boolean yes;
  69.     RgnHandle rgn;
  70.     
  71.     #if PROFILE
  72.         InitProfile(200,3);    /* only needed if you want timing info */
  73.         _profile=0;
  74.     #endif
  75.     /* INITIALIZE QuickDraw */
  76.     #if THINK_C
  77.         console_options.title="\pFlickeringGrating";
  78.         console_options.top = 20;
  79.         console_options.nrows = 4;
  80.         printf("\n");
  81.     #else
  82.         InitGraf((Ptr) &thePort);
  83.         InitFonts();
  84.         InitWindows();
  85.         InitCursor();
  86.     #endif
  87. //    HideMenuBar();
  88.     printf("Welcome to FlickeringGrating.\n");
  89.     GetPort(&oldWindow);
  90.     for(i=8;i>=0;i--){
  91.         // look for a screen with 8-bit pixels.
  92.         device=GetScreenDevice(i);
  93.         if(device == NULL)continue;
  94.         pixelSize=(*(*device)->gdPMap)->pixelSize;
  95.         if(pixelSize==8)break;
  96.     }
  97.     if(pixelSize!=8 && NewPaletteManager())for(i=8;i>=0;i--){
  98.         // try to force a screen to 8-bit pixels.
  99.         device=GetScreenDevice(i);
  100.         if(device == NULL)continue;
  101.         SetDepth(device,8,1L<<gdDevType,1);
  102.         pixelSize=(*(*device)->gdPMap)->pixelSize;
  103.         if(pixelSize==8)break;
  104.     }
  105.     if(device==NULL || pixelSize != 8) PrintfExit("Sorry, I require 8 bits/pixel.\n");
  106.     window = GDOpenWindow1(device);
  107.     sprintf(string,"LuminanceRecord%d.h",i);
  108.     i=ReadLuminanceRecord(string,&LR,0);    /* try to read correct file */
  109.     if(i<=0){
  110.         #include "LuminanceRecord1.h"
  111.     }
  112.     printf("Using luminance calibration for screen %d calibrated %s by %s.\n"
  113.         ,LR.screen,LR.date,LR.notes);
  114.     printf("Have you installed an ISR Video Attenuator?");
  115.     yes=YesOrNo(0);
  116.     printf("\n");
  117.     if(!yes){
  118.         LR.r=0.0;
  119.         LR.g=1.0;
  120.         LR.b=0.0;
  121.     }
  122.     #if PROFILE
  123.         _profile=1;
  124.     #endif
  125.  
  126.     /* load clut with linear gray scale */
  127.     // We'll leave clut entries 0 (white) and clutSize-1 (black) alone,
  128.     // since they are used heavily by Apple's stuff. Window frames
  129.     // will mostly look normal if we leave those two entries alone.
  130.     clutSize=GDClutSize(device);
  131.     SetLuminances(device,&LR,1,clutSize-2,LR.LMin,LR.LMax);
  132.         
  133.     /* Display a sinusoid with a gaussian envelope */
  134.     // Compute the image.
  135.     SetPort(window);
  136.     PmBackColor(1+(long)(0.5+(clutSize-3)*0.5));
  137.     EraseRect(&window->portRect);
  138.     SetRect(&r,0,0,SIZE,SIZE);
  139.     CenterRectInRect(&r,&window->portRect);
  140.     for(i=0;i<SIZE;i++){
  141.         a=(i-SIZE/2)/(SIZE/6.);
  142.         fY[i]=exp(-a*a);
  143.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  144.     }
  145.     for(j=0;j<SIZE;j++){
  146.         unsigned long row[SIZE];
  147.         for(i=0;i<SIZE;i++) row[i]=1+(long)(0.5+(clutSize-3)*0.5*(1.0+fY[j]*fX[i]));
  148.         SetPixelsQuickly(r.left,j+r.top,row,SIZE);
  149.     }
  150.     // These bits of conditional code force the Radius PowerView
  151.     // to update the screen from the video buffer in memory.
  152.     #if 0    // No good; causes undesired color translation.
  153.         oldDevice=GetGDevice();
  154.         SetGDevice(device);
  155.         CopyBits((BitMap *)*((CGrafPtr)window)->portPixMap
  156.             ,(BitMap *)*((CGrafPtr)window)->portPixMap
  157.             ,&r,&r,srcCopy,NULL);
  158.         SetGDevice(oldDevice);
  159.     #endif
  160.     #if 1    // Ok, but I don't actually want to scroll.
  161.         rgn=NewRgn();
  162.         ScrollRect(&r,0,1,rgn);
  163.         DisposeRgn(rgn);
  164.     #endif
  165.     // Allocate ColorSpec tables, one for each frame
  166.     for(i=0;i<TMAX;i++){
  167.         tables[i]= (ColorSpecTable *)NewPtr(sizeof(ColorSpecTable));
  168.         if(tables[i]==NULL){
  169.             printf("Only room for %d lookup tables (one per frame) ... continuing.\n",i);
  170.             break;
  171.         }
  172.     }
  173.     tmax=i;
  174.     // Compute lookup tables.
  175.     LMid=(LR.LMax+LR.LMin)/2.0;
  176.     contrast=(LR.LMax-LR.LMin)/(LR.LMax+LR.LMin);
  177.     LMax=LMid*(1.0+contrast);
  178.     LMin=LMid*(1.0-contrast);
  179.     for(i=0;i<tmax;i++){
  180.         a=6.0*(i-tmax/2)/tmax;
  181.         dL=LMid*contrast*exp(-a*a)*sin((i-tmax/2)*(2.0*PI*3.0*0.015));
  182.         SetLuminancesAndRange(NULL,&LR,1,clutSize-2,LMid-dL,LMid+dL,LMin,LMax);
  183.         *tables[i]= *(ColorSpecTable *)LR.table;
  184.     }
  185.     printf("Now displaying the animation ...\n");
  186.     for(j=0;j<REPETITIONS;j++){
  187.         for(i=0;i<tmax;i++){
  188.             // This is a thinly disguished call to GDSetEntries.
  189.             LoadLuminances(device,(luminanceRecord *) tables[i],1,clutSize-2);
  190.         }
  191.     }
  192.     #if PROFILE
  193.         _profile=0;
  194.     #endif
  195.     for(i=0;i<tmax;i++)DisposPtr((Ptr)tables[i]);
  196.     SetPort(oldWindow);
  197.     GDDisposeWindow1(window);
  198.     RestoreCluts();
  199. //    ShowMenuBar();
  200.     #if !PROFILE
  201.         abort();
  202.     #endif
  203. }
  204.